﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using TS = TransactionServer.General_Classes;
using Microsoft.Xrm.Sdk;

namespace TransactionServer.CRMActivities
{
    
    public sealed class CRMGetPicklistValue : CodeActivity
    {

        // Define an activity input argument of type string
        public InArgument<TS.Context> Context { get; set; }
        public InArgument<string> Connection { get; set; }
        public InArgument<string> EntityName { get; set; }
        public InArgument<string> AttributeName { get; set; }
        public InArgument<string> OptionSet { get; set; }
        public InArgument<string> ValueName { get; set; }
        public InArgument<bool> CreateNew { get; set; }
        public OutArgument<string> IntValue { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string connection = context.GetValue(this.Connection);
            string entityName = context.GetValue(this.EntityName);
            string attributeName = context.GetValue(this.AttributeName);
            string optionSet = context.GetValue(this.OptionSet);
            string valueName = context.GetValue(this.ValueName);
            bool createNew = context.GetValue(this.CreateNew);
            TS.Context con = context.GetValue(this.Context);
            CRMConnector conn = (CRMConnector)con.Connector[connection];

            int result = conn.getAttributePicklistValue(entityName, attributeName, optionSet, valueName, createNew);
            
            context.SetValue(this.IntValue, result.ToString());
        }
    }
}
